/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javamail;

import javamail.speech.speechSynthesizer;
import java.io.IOException;
import java.io.File;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import javamail.common.logAllLevels;
import javamail.config.cliParms;
import javamail.config.config;
import javamail.sip.sipUA;

/**
 *
 * @author sriram
 */
public class main {
    private static Logger logger = logAllLevels.getLogger();
    private static FileHandler fh = null;
    private static cliParms cliparms = null;
    public static config glblConfig = null;
    private static String defaultMessage = "Welcome to java speech server. You have no messages now!";
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //speechSynthesizer gen = new speechSynthesizer();
        //gen.synthesize("Hi, Welcome to your voice mailbox. You currently have no messages, buzz off", "currentmessage");
        // Start the web server with specified port
        try {
            setupLogger("javamail.log");
            if(args.length%2 != 0) {
                logger.severe("Error in CLI parameters");
                logger.severe("Usage: javamail -c config_file_name -user sip_uri -http http_bind_address");
                return;
            } else {
                cliparms = new cliParms(args);
                if(cliparms.isError()) {
                    logger.severe("Error in CLI parameters");
                    logger.severe("Usage: javamail -c config_file_name -user sip_uri -http http_bind_address");
                    return;                    
                }
            }
            // Load the global configuration from the CLI parameters
            glblConfig = new config(cliparms);
            if(glblConfig==null) {
                logger.severe("Unable to retreive configuration! Exiting the application");
            }
            else {
                String wav = glblConfig.getDefaultMessage();
                File f = new File(wav);
                if(!f.exists()) {
                    speechSynthesizer gen = new speechSynthesizer();
                    gen.synthesize(defaultMessage, wav);
                }
                String currentWav = glblConfig.getCurrentMessage();
                String currentTxt = glblConfig.getMessageText();
                if(currentTxt!=null && !currentTxt.trim().equals("")) {
                    f = new File(currentWav);
                    if(!f.exists()) {
                        speechSynthesizer gen = new speechSynthesizer();
                        gen.synthesize(currentTxt, currentWav);
                    }
                }
                // Start the sip user agent
                startSipServer();
                // Start the web server
                startWebServer();
            }
        } catch (Exception e) {
            logger.severe("Program crasheD!");
            e.printStackTrace();
        }
    }
    
    public static void startWebServer() {
        webServer wServer = new webServer();
        wServer.start();
    }
    
    public static void startSipServer() {
        sipUA sipUA = new sipUA();
        sipUA.start();
    }

    
    public static void setupLogger(String name) {
        try {
            fh = new FileHandler("log/" + name);
            fh.setFormatter(new SimpleFormatter());
            logger.addHandler(fh);
        } catch (IOException e) {
            logger.info("Directory Log being created...");
            String dir = new String("log");
            boolean success = (new File(dir)).mkdir();
            if(success) {
                try {
                    fh = new FileHandler("log/javamail.log");
                    fh.setFormatter(new SimpleFormatter());
                    logger.addHandler(fh);
                } catch (Exception err) {
                    logger.warning("Unable to create log file!" + err.getMessage());
                }
            }
        }
        // Set logger level to ALL
        logger.setLevel(Level.ALL);
    }
}
